package com.example.skin_whitening_ndk;

import android.graphics.Bitmap;
import android.graphics.Color;

/**
 * Created by jiang at 2021/9/26
 */
public class javaBitmapToGray {
    private  static int Pixel_Alpha=0;
    private static int Pixel_Red=0;
    private  static int Pixel_Green=0;
    private  static int Pixel_Blue=0;

    public static Bitmap getImageBitmap(Bitmap src){
        int width=src.getWidth();
        int height=src.getHeight();
        Bitmap dst=Bitmap.createBitmap(width,height, Bitmap.Config.RGB_565);

        int alpha,red,green,blue;

        int Pixel_color;
        int gray;
        //依次拿到每一个像素点
        for (int x=0;x<width;x++){
            for (int y=0;y<height;y++){
                Pixel_color= src.getPixel(x,y);
                alpha= Color.alpha(Pixel_color);//得到透明度
                red=Color.red(Pixel_color);//得到红色
                green=Color.green(Pixel_color);//得到绿色
                blue=Color.blue(Pixel_color);//得到蓝色

                gray=getAddAvg(red,green,blue);//转成灰色图像

                //合成像素
                dst.setPixel(x,y,Color.argb(alpha,gray,gray,gray));
            }
        }
        return dst;
    }
    //最大值法灰度化,比较三个数的大小
    public static int getBigger(int r,int g,int b){
        if(r>=g&&r>=b){
            return r;
        }else if(g>=r&&g>=b){
            return g;
        }else if(b>=r&&b>=g){
            return b;
        }else{
            return 0;
        }
    }
    //最小值法灰度化,比较三个是的大小取最小数
    public static int getSmall(int r,int g,int b){
        if(r<=g&&r<=b){
            return r;
        }else if(g>=r&&g>=b){
            return g;
        }else if(b>=r&&b>=g){
            return b;
        }else{
            return 0;
        }
    }

    //均值法
    public static int getAvg(int r,int g,int b){
        int avg=(r+g+b)/3;
        return avg;
    }
    //加权平均法
    public static int getAddAvg(int r,int g,int b){
        int addavg=(int) (0.299*r+0.578*g+0.114*b);
        return addavg;
    }
}
